#include <sqlite3.h>
#include <stdio.h>

/*
ID	名字	年龄
1	张三	32	
2	李四	33	
3	王五	30	
4	王博	32	
5	李为	33	
6	赵倩	30	
Operation done successfully 

 */

// 回调函数
int callback(void *data, int argc, char **argv, char **azColName) {
	int i;
	if(argv[0][0] == '1')
		printf("%s\n", (char *)data);
	for(i = 0; i < argc; i++) {
		printf("%s\t", argv[i] ? argv[i] : "NULL");
	}
	printf("\n");

	return 0;
}

int main () {
	sqlite3 *db;
	char *zErrMsg = 0; // 存储错误消息的指针
	int rc;

	rc = sqlite3_open("test.db", &db);
	if (rc != SQLITE_OK) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}

	char *str = "ID\t名字\t年龄";
	char *sql = "SELECT * from COMPANY";
	rc = sqlite3_exec(db, sql, callback, str, &zErrMsg);

	if (rc != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	} else {
		printf("Operation done successfully\n");
	}

	sqlite3_close(db);
	return rc;
}
